import cv2
import os  # 添加缺失的os模块

def on_mouse(event, x, y, flags, param):  # 移除错误的self参数
    global img, img2, point1, point2
    img2 = img.copy()
    if event == cv2.EVENT_LBUTTONDOWN:
        point1 = (x, y)
    elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):
        cv2.rectangle(img2, point1, (x,y), (255,0,0), 2)
        cv2.imshow('image', img2)
    elif event == cv2.EVENT_LBUTTONUP:
        point2 = (x,y)
        cv2.rectangle(img2, point1, point2, (0,0,255), 5)
        cv2.imshow('image', img2)
        min_x = min(point1[0], point2[0])
        min_y = min(point1[1], point2[1])
        width = abs(point1[0] - point2[0])
        height = abs(point1[1] - point2[1])
        cut_img = img[min_y:min_y+height, min_x:min_x+width]
        cv2.imwrite("E:/ningyuan/sumimoto/Curelasto/tmp/cut.png", cut_img)
        cv2.destroyWindow("image")
        os.remove("E:/ningyuan/sumimoto/Curelasto/tmp/scr.png")

img = cv2.imread("E:/ningyuan/sumimoto/Curelasto/tmp/scr.png")
cv2.namedWindow('image', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty('image', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback('image', on_mouse)
cv2.imshow('image', img)  # 修正self.img为img
cv2.waitKey(0)